package cn.bs.busuo.front.account.controller;

import cn.bs.busuo.front.account.pojo.param.UserAddNewParam;
import cn.bs.busuo.front.account.pojo.param.UserBasicUpdateParam;
import cn.bs.busuo.front.account.pojo.vo.UserStandardVO;
import cn.bs.busuo.front.account.service.IUserService;
import cn.bs.busuo.common.pojo.vo.PageData;
import cn.bs.busuo.common.validation.account.UserRules;
import cn.bs.busuo.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@RestController
@Slf4j
@RequestMapping("/front/users")
@Api(tags = "1.用户管理")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 注册用户的接口
     */
    @PostMapping("/add-new")
    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid UserAddNewParam userAddNewParam) {
        userService.addNew(userAddNewParam);
        return JsonResult.ok();
    }

//    /**
//     * 管理员根据ID删除用户
//     */
//    @PostMapping("/{id:[0-9]+}/delete")
//    @PreAuthorize("hasAuthority('/account/user/system')")
//    @ApiOperation("管理员根据ID删除用户(逻辑删除)")
//    @ApiOperationSupport(order = 200)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
//    })
//    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
//        userService.delete(id);
//        return JsonResult.ok();
//    }

    /**
     * 用户修改用户基本信息
     */
    @PostMapping("/{id:[0-9]+}/info/update")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户修改基本信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateInfo(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                 @Valid UserBasicUpdateParam userBasicUpdateParam) {
        userService.updateInfo(id, userBasicUpdateParam);
        return JsonResult.ok();
    }

    /**
     * 用户修改密码
     */
    @PostMapping("/{id:[0-9]+}/password/update")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户修改密码")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")
    })
    public JsonResult updatePassword(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String newPassword) {
        userService.updatePassword(id, newPassword);
        return JsonResult.ok();
    }

//    /**
//     * 管理员启用用户
//     */
//    @PostMapping("/{id:[0-9]+}/enable")
//    @PreAuthorize("hasAuthority('/account/user/system')")
//    @ApiOperation("管理员启用用户")
//    @ApiOperationSupport(order = 350)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
//    })
//    public JsonResult setEnable(@PathVariable @Range(min = 2, message = "请提交合法的用户ID值！") Long id) {
//        userService.setEnable(id);
//        return JsonResult.ok();
//    }
//
//    /**
//     * 管理员禁用用户
//     */
//    @PostMapping("/{id:[0-9]+}/disable")
//    @PreAuthorize("hasAuthority('/account/user/system')")
//    @ApiOperation("管理员禁用用户")
//    @ApiOperationSupport(order = 351)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
//    })
//    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
//        userService.setDisable(id);
//        return JsonResult.ok();
//    }

    /**
     * 根据ID查询用户
     */
    @ApiOperation("根据ID查询用户")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
        UserStandardVO tag = userService.getStandardById(id);
        return JsonResult.ok(tag);
    }

    /**
     * 查询用户列表
     */
    @GetMapping("")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult list(Integer page) {
        Integer pageNum = page == null ? 1 : page;
        PageData<UserStandardVO> pageData = userService.list(pageNum);
        return JsonResult.ok(pageData);
    }





}
